# -*- encoding:utf-8 -*-

from __future__ import unicode_literals
from django.db import models
from django.conf import settings
from django.contrib.admin.models import LogEntry
from django.utils import timezone

# Create your models here.

class TempModel(models.Model):
    name=models.CharField('name', max_length=100,default='')
    screenshot= models.CharField('screen shot url', max_length=400, default='')
    dsp = models.CharField('description', max_length=50, blank=True)
    
    def __str__(self):
        return self.name
    

class Studio(models.Model):
    name = models.CharField('name', max_length=300, blank=True)
    logo = models.CharField('Logo', max_length=400, default='')
    slogan = models.CharField('Slogan', max_length=400, blank=True)
    dsp = models.TextField(verbose_name='Introduction', default='')
    link_ios = models.CharField('link to Apple Store',max_length=400, default='')
    link_gp = models.CharField('link to Google Play',max_length=400, default='')
    link_face = models.CharField('link to Facebook',max_length=400, blank=True)
    link_twit = models.CharField('link to Twitter',max_length=400, blank=True)
    email = models.CharField('Contact US Email', max_length=100, default='')
    policy = models.TextField(verbose_name='Private policy', blank=True)
    ga_key = models.CharField('Google Analytics Key', max_length=300, default='')
    fea_app = models.TextField(verbose_name='Featured App', blank=True)
    copyright = models.CharField('Copyright', max_length=400, default='')
    favicon =  models.CharField('Favicon', max_length=400, blank=True)
    temp = models.ForeignKey(TempModel,verbose_name='Template',null=True,blank=True)
    deploy_account = models.ForeignKey('DeployAccount',verbose_name='Deploy Account', blank=True, null=True)
    auto_deploy = models.CharField('Auto Deploy', max_length=100, default='no')
    domain = models.CharField('Studio Domain Name', max_length=300, blank=True)
    code_inject=models.TextField(verbose_name='Code Inject',blank=True )
    last_save=models.DateTimeField(verbose_name='Last Save time',auto_now=True,blank=True,null=True)
    
    
    def is_deployable(self):
        for k in self._meta.fields:
            if k.blank==False and not getattr(self,k.name):
                return False
        return True
        
    @property
    def freeze(self):
        """
        replace by status 
        TODO: remove this method
        """        
        if hasattr(self,'freezemodel'):
            return self.freezemodel
        else:
            obj = FreezeModel.objects.create(studio=self)
            return obj
    
    @property
    def status(self):
        if hasattr(self,'freezemodel'):
            return self.freezemodel
        else:
            obj = FreezeModel.objects.create(studio=self)
            return obj        


class FreezeModel(models.Model):
    """
    """
    studio = models.OneToOneField(Studio,verbose_name='freezed Studio', blank=True, null=True)
    # status = models.CharField('Freeze Status',max_length=150, blank=True)
    appset = models.CharField('is app set', max_length=100, default='no')
    last_zip =models.DateTimeField(verbose_name='Last zip time',blank=True,null=True)
    last_deploy=models.DateTimeField(verbose_name='Last Deploy time',blank=True,null=True)
    
    def done_update_appset(self,info='yes'):
        self.appset=info
        self.save()
        name=self.studio.name
        l=Log(category='studio.update',key=name,type='normal',title='update studio {name}'.format(name=name))
        l.save()  
            
    def done_zip(self):
        """
        log and record time
        """
        self.last_zip=timezone.now()
        self.save()
        name=self.studio.name
        l=Log(category='studio.zip',key=name,type='normal',title='zip success')
        l.save()        

    
    def done_deploy(self):
        self.last_deploy=timezone.now()
        self.save()
        name=self.studio.name
        l=Log(category='studio.deploy',key=name,type='normal',title='deploy success')
        l.save()
    
    

class AppModel(models.Model):
    StatusChoice=(
        ('normal','Normal'),
        ('warning','Warning'), 
        ('error','Error'),
        ('deactive','Deactive')
    )
    Orientation=(
        ('portait','Portait'),
        ('landscape','Landscape')
    )
    sku = models.CharField('SKU', max_length=300, default='')
    app_code = models.CharField('App Code', max_length=300, blank=True)
    name = models.CharField('App Name', max_length=500, blank=True)
    pub_name = models.CharField('App pub name', max_length=500, blank=True)
    studio= models.ForeignKey(Studio,verbose_name='Studio',blank=True,null=True)
    icon = models.CharField('Icon Url', max_length=300, blank=True)
    screenshots = models.TextField(verbose_name='Screen Shot', blank=True)
    fea_graph = models.CharField('Featured Graphics', max_length=300, blank=True)
    long_dsp = models.TextField(verbose_name='long meta info', blank=True)
    short_dsp = models.TextField(verbose_name='short meta info', blank=True)
    gp_link = models.CharField('google play link', max_length=500, blank=True)
    status=models.CharField('Status', max_length=100, choices=StatusChoice, blank=True)
    orientation=models.CharField('orientation', max_length=100, choices=Orientation, blank=True)
    date_released = models.CharField('date released', max_length=100, blank=True)
    
    def __unicode__(self):
        return self.name or self.app_code


class Log(models.Model):
    LOG_Type=(
        ('normal','Normal'),
        ('warning','Waring'),
        ('error','Error')
    )
    category = models.CharField('Log Category', max_length=100, default='')
    key = models.CharField('Log Key', max_length=100, blank=True)
    type = models.CharField('Status', max_length=100, choices=LOG_Type,default='normal')
    user = models.ForeignKey(settings.AUTH_USER_MODEL,blank=True, null=True)
    time = models.DateTimeField(verbose_name='date time',auto_now=True)
    title = models.CharField('title', max_length=400, blank=True)
    content = models.TextField(verbose_name='Log Content', blank=True)
    
    
class DeployAccount(models.Model):
    account=models.CharField('Account', max_length=200, default='')
    pswd = models.CharField('PassWord', max_length=200, default='')
    
    def __unicode__(self):
        return self.account
    
    
    
    
    
    
    